package com.epam.facebook_client.task;

import java.io.IOException;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;

import android.content.Context;
import android.os.AsyncTask;
import android.view.View;

public abstract class CommonPostAsyncTask extends
		AsyncTask<String, Void, String> {
	private Exception mError;
	
	protected Context context;
	protected View view;


	public CommonPostAsyncTask(Context context) {
		this(context, null);
	}

	public CommonPostAsyncTask(Context context, View view) {
		this.context = context;
		this.view = view;
	}

	@Override
	protected void onPreExecute() {
		if (view != null) {
			view.setVisibility(View.VISIBLE);
		}
	}

	@Override
	protected String doInBackground(String... arg) {
		try {
			return post(arg[0]);
		} catch (ClientProtocolException e) {
			mError = e;
		} catch (IOException e) {
			mError = e;
		}
		return null;
	}

	@Override
	protected void onPostExecute(String result) {
		if (mError != null) {
			onError(mError);
		} else {
			if (view != null) {
				view.setVisibility(View.GONE);
			}
			success(result);
		}
	}

	public abstract String post(String url) throws ClientProtocolException, IOException;
	
	public abstract void success(String result);

	public abstract Map<String, String> params();

	public abstract void onError(Exception e);

}
